package com.vitogiovanni.tobook.servlet;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.vitogiovanni.tobook.entity.CategoriaElemento;
import com.vitogiovanni.tobook.entity.CategoriaElementoEndpoint;
import com.vitogiovanni.tobook.entity.ElementoMenu;
import com.vitogiovanni.tobook.entity.Menu;
import com.vitogiovanni.tobook.entity.MenuEndpoint;
import com.vitogiovanni.tobook.entity.TipoElemento;
import com.vitogiovanni.tobook.entity.TipoElementoEndpoint;

/**
 * Servlet implementation class InvitaLavoroServlet
 */
public class InserisciElementoMenuServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public InserisciElementoMenuServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		//definizione di Endpoint e di Entity
		ElementoMenu elementoMenu = new ElementoMenu();
		
		MenuEndpoint menuEndpoint = new MenuEndpoint();
		Menu menu = new Menu();
		
		TipoElementoEndpoint tipoElementoEndpoint = new TipoElementoEndpoint();
		TipoElemento tipoElemento = new TipoElemento();
		
		CategoriaElementoEndpoint categoriaElementoEndpoint = new CategoriaElementoEndpoint();
		CategoriaElemento categoriaElemento = new CategoriaElemento();
		
		String nome = req.getParameter("nome");
		String ingredienti = req.getParameter("ingredienti");
		String menuId = req.getParameter("menu");
		String tipoElementoName = req.getParameter("tipoElemento");
		String[] categoriaElementoNome = req.getParameterValues("categoriaElemento");
		String prezzoElemento = req.getParameter("prezzo");
		
		menu = menuEndpoint.findByNome(menuId);
		List<ElementoMenu> elementiMenu = menu.getElementiMenu();
		
		tipoElemento = tipoElementoEndpoint.findByName(tipoElementoName);
		
		Set<Key> categorie = new HashSet<Key>();
		
		if (categoriaElementoNome.length!=0) {
			for (String temp : categoriaElementoNome) {
				if (!temp.equals("")) {
					categoriaElemento = categoriaElementoEndpoint.findByName(temp);
					categorie.add(categoriaElemento.getId());
				}
			}
		}
		
		elementoMenu.setNome(nome);
		elementoMenu.setIngredienti(ingredienti);
		elementoMenu.setMenu(menu);
		elementoMenu.setTipoElemento(tipoElemento);
		elementoMenu.setCategoriaElemento(categorie);
		elementoMenu.setPrezzoElemento(Double.valueOf(prezzoElemento));
		
		elementiMenu.add(elementoMenu);
		menu.setElementiMenu(elementiMenu);
		
		menuEndpoint.updateMenu(menu);
		//menuEndpoint.addElementoMenu(menu.getId().getId(), elementoMenu);
	
		resp.sendRedirect("/inserimento.html");
	
	}
}